package com.canbot.u05.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.canbot.u05.R;
import com.canbot.u05.control.Cmd;
import com.canbot.u05.control.RobotMgr;
import com.canbot.u05.fragment.ActionFragment;
import com.canbot.u05.fragment.DanceFragment;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;

import java.util.ArrayList;
import java.util.List;

/**
 * 舞蹈演示界面
 */

public class ActionDanceActivity extends FragmentActivity implements View.OnClickListener {

        private ViewPager mViewPager;

        private List<Fragment> mFragmentList = new ArrayList<>();

        private ImageView imageView;

        private int pressNum=0;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_action_dance);
                hideBottomUIMenu();
                mViewPager = (ViewPager) findViewById(R.id.viewpager);
                findViewById(R.id.btn_back_handle_action).setOnClickListener(this);
                imageView = (ImageView) findViewById(R.id.image_action_dance);
                mFragmentList.add(new DanceFragment());
                mFragmentList.add(new ActionFragment());
                mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), mFragmentList));
                mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int i, float v, int i1) {
                        }

                        @Override
                        public void onPageSelected(int i) {
                                if(i==0){
                                        imageView.setImageDrawable(getResources().getDrawable(R.drawable.btn_dance_sel));
                                }else{
                                        imageView.setImageDrawable(getResources().getDrawable(R.drawable.btn_action_sel));
                                }

                        }

                        @Override
                        public void onPageScrollStateChanged(int i) {

                        }
                });
                mViewPager.setCurrentItem(0);//设置当前显示标签页为第一页

        }


        class FragmentAdapter extends FragmentPagerAdapter {

                List<Fragment> fragmentList = new ArrayList<Fragment>();

                public FragmentAdapter(FragmentManager fm,
                                       List<Fragment> fragmentList) {
                        super(fm);
                        this.fragmentList = fragmentList;
                }

                @Override
                public Fragment getItem(int position) {
                        return fragmentList.get(position);
                }

                @Override
                public int getCount() {
                        return fragmentList.size();
                }
        }


        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.btn_back_handle_action:
                                //停止播放音频
                                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                                MsgSendUtils.sendStringMsgToPad(MsgType.SEND_PAD_STOP_ACTION, "stopAction");
                                RobotMgr.getInstance().keyControl(Cmd.Key_tingzhi);
                                finish();
                                break;
//                        case R.id.btn_action_handle_dance:
//                                finish();
//                                Intent intent=new Intent(ActionDanceActivity.this,HandleTelecontrolActivity.class);
//                                startActivity(intent);
//                                break;
                        default:
                                break;
                }

        }

        public void actionTodance(View view) {
                ImageView imageView = (ImageView) findViewById(R.id.image_action_dance);
                if (pressNum % 2 == 0) {
                        imageView.setImageDrawable(getResources().getDrawable(R.drawable.btn_action_sel));
                        mViewPager.setCurrentItem(1);
                        pressNum++;
                }
                else {
                        imageView.setImageDrawable(getResources().getDrawable(R.drawable.btn_dance_sel));
                        mViewPager.setCurrentItem(0);
                        pressNum++;
                }
        }

        /**
         * 隐藏虚拟按键，并且全屏
         */
        protected void hideBottomUIMenu() {
                //隐藏虚拟按键，并且全屏
                //for new api versions.
                View decorView = getWindow().getDecorView();
                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
                decorView.setSystemUiVisibility(uiOptions);
        }

}
